-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
75 lines (59 loc) · 1.86 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;
class TreeNode {
int value;
TreeNode left, right;
TreeNode(int value) {
this.value = value;
left = right = null;
}
}
public class TreeTraversals {
// Preorder Traversal
public static void preorder(TreeNode root) {
if (root != null) {
System.out.print(root.value + " ");
preorder(root.left);
preorder(root.right);
}
}
// Inorder Traversal
public static void inorder(TreeNode root) {
if (root != null) {
inorder(root.left);
System.out.print(root.value + " ");
inorder(root.right);
}
}
// Postorder Traversal
public static void postorder(TreeNode root) {
if (root != null) {
postorder(root.left);
postorder(root.right);
System.out.print(root.value + " ");
}
}
// Build a sample tree for demonstration
public static TreeNode buildTree(Scanner scanner) {
System.out.print("Enter value (-1 for no node): ");
int value = scanner.nextInt();
if (value == -1) return null;
TreeNode node = new TreeNode(value);
System.out.println("Enter left child of " + value + ":");
node.left = buildTree(scanner);
System.out.println("Enter right child of " + value + ":");
node.right = buildTree(scanner);
return node;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Build the binary tree:");
TreeNode root = buildTree(scanner);
System.out.println("\nPreorder Traversal:");
preorder(root);
System.out.println("\nInorder Traversal:");
inorder(root);
System.out.println("\nPostorder Traversal:");
postorder(root);
scanner.close();
}
}